﻿using Common.Utils;
using Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Models
{
	public class GymModel
	{
		private Storage.Accessors.ExerciseAccessor _accessor;

		private AwaitLock _lock = AwaitLock.CreateGlobal();

		public GymModel(Storage.Repositories.Base.RepStorage storage)
		{
			_accessor = new Storage.Accessors.ExerciseAccessor(storage.ExerciseRepo, storage.ExerciseLogRepo, storage.SetLogRepo);
		}

		public async Task<List<Exercise>> LoadAllAsync()
		{
			using (await _lock.Lock())
			{
				return await _accessor.GetAll();
			}
		}

		public async Task<List<ExerciseLookup>> LoadAllLookupsAsync()
		{
			using (await _lock.Lock())
			{
				return await _accessor.GetExerciseLookups();
			}
		}

		public async Task SaveSetLogAsync(Guid exerciseLogId, SetLog setLog)
		{
			using (await _lock.Lock())
			{
				await _accessor.SaveSetLogAsync(exerciseLogId, setLog);
			}
		}

		public async Task UpdateSetLogAsync(SetLog setLog)
		{
			if (setLog == null || setLog.Id == Guid.Empty)
				return;

			using (await _lock.Lock())
			{
				await _accessor.UpdateSetLogAsync(setLog);
			}
		}

		public async Task RemoveSetLogAsync(SetLog setLog)
		{
			if (setLog == null || setLog.Id == Guid.Empty)
				return;

			using (await _lock.Lock())
			{
				await _accessor.RemoveSetLogAsync(setLog.Id);
			}
		}

		public async Task CreateExerciseLogAsync(Guid exerciseId, ExerciseLog exerciseLog)
		{
			using (await _lock.Lock())
			{
				await _accessor.SaveExerciseLogAsync(exerciseId, exerciseLog);
			}
		}

		public async Task ClearAllAsync()
		{
			using (await _lock.Lock())
			{
				await _accessor.Clear();
			}
		}

		public async Task SaveExerciseAsync(Exercise exercise)
		{
			using (await _lock.Lock())
			{
				await _accessor.SaveExerciseAsync(exercise);
			}
		}

		public async Task<List<ExerciseLog>> GetExerciseLogsAsync(Guid exerciseId)
		{
			using (await _lock.Lock())
			{
				return await _accessor.GetExerciseLogsAsync(exerciseId);
			}
		}

		public async Task<Exercise> LoadExerciseByIdAsync(Guid exerciseId)
		{
			Exercise res = null;
			using (await _lock.Lock())
			{
				res = await _accessor.GetExerciseByIdAsync(exerciseId);
			}			
			return res;
		}


		public async Task RemoveExerciseAsync(Guid exerciseId)
		{
			using (await _lock.Lock())
			{
				await _accessor.RemoveExerciseAsync(exerciseId);
			}
		}

		public async Task SaveExerciseDisplayNameAsync(Guid ExerciseId, string displayName)
		{
			using (await _lock.Lock())
			{
				await _accessor.UpdateExerciseDisplayNameAsync(ExerciseId, displayName);
			}
		}

        public async Task<ExerciseLookup> LoadLookupAsync(Guid exerciseId)
        {
            using (await _lock.Lock())
			{
                return await _accessor.GetExerciseLookup(exerciseId);
			}
        }
    }
}
